﻿@model TaxonomyFieldViewModel
@using Orchard.Taxonomies.Helpers;
@using Orchard.Utility.Extensions;
@using Orchard.Taxonomies.ViewModels;

@{
    Style.Include("admin-taxonomy.css");
    Script.Require("jQuery"); 
    Script.Include("~/Themes/TheAdmin/scripts/admin.js").AtFoot();
    Script.Include("admin-taxonomy-expando.js").AtFoot();

    int termIndex = 0;
    var settings = Model.Settings;
}

<fieldset class="taxonomy-wrapper" data-name-prefix="@Html.FieldNameFor(m => m)" data-id-prefix="@Html.FieldIdFor(m => m)">
    <legend @if(settings.Required) { <text>class="required"</text> }>@Model.DisplayName.CamelFriendly()</legend>
    
    <div class="expando">
        @if (!String.IsNullOrWhiteSpace(Model.Settings.Hint))
        {
            <span class="hint">@Model.Settings.Hint</span>
        }
        
        <ul class="terms">
            @foreach (var entry in Model.Terms) {
                var ti = termIndex;
                <li>
                    @* Tabs for levels *@ @for (var i = 1; i <= entry.GetLevels(); i++){ <span class="gap">&nbsp;</span> }
                    @{
                        var disabled = !entry.Selectable || (Model.Settings.LeavesOnly && Model.Terms.Any(t => t.Path.Contains(entry.Path + entry.Id)));
                        if (Model.Settings.SingleChoice) {
                        <input @if (disabled){ <text>disabled="disabled"</text> } type="radio" value="@Model.Terms[ti].Id" @if (entry.Id == Model.SingleTermId) { <text>checked="checked"</text> } name="@Html.FieldNameFor(m => m.SingleTermId)" id="@Html.FieldIdFor(m => m.Terms[ti].IsChecked)" data-term="@entry.Name.ToLower()"/> 
                        }
                        else {
                        <input @if (disabled) { <text>disabled="disabled"</text> } type="checkbox" value="true" @if (entry.IsChecked){ <text>checked="checked"</text> } name="@Html.FieldNameFor(m => m.Terms[ti].IsChecked)" id="@Html.FieldIdFor(m => m.Terms[ti].IsChecked)" data-term="@entry.Name.ToLower()"/> 
                        }
                    }
                    @Html.HiddenFor(m => m.Terms[ti].Id)
                    <label class="forcheckbox" for="@Html.FieldIdFor(m => m.Terms[ti].IsChecked)">@entry.Name</label>
                </li>
                termIndex++;
            }
        </ul>
            
        @if (!Model.Terms.Any()) {
            <div class="no-terms">
                @T("There are no terms defined for {0} yet.", Model.DisplayName.CamelFriendly())
                <a href="@Url.Action("Index", "TermAdmin", new { taxonomyId = Model.TaxonomyId, area = "Orchard.Taxonomies" })">@T("Create some terms")</a>
            </div>        
        }
    </div>
    @Html.HiddenFor(m => m.TaxonomyId)
</fieldset>
